﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

using LowLevelGraphics.Filter;

namespace ImageRecognition2.Forms
{
    /// <summary>
    /// Form to change hue/saturation/lightness
    /// </summary>
    public partial class FormHueSaturationLightness : FormFilter
    {
        protected AhsbColor m_AhsbColor = new AhsbColor();

        /// <summary>
        /// Initializes a new instance of the <see cref="FormHueSaturation"/> class.
        /// </summary>
        public FormHueSaturationLightness()
        {
            InitializeComponent();

            this.pictureBox1.BackgroundImageLayout = ImageLayout.Zoom;
            this.trackBarHue.ValueChanged += new EventHandler(trackBar_Scroll);
            this.trackBarHue.Tag = tbHue;
            this.tbHue.ValueChanged += new EventHandler(nudValue_Changed);
            this.tbHue.Tag = trackBarHue;

            this.trackbLightness.ValueChanged += new EventHandler(trackBar_Scroll);
            this.trackbLightness.Tag = tbLightness;
            this.tbLightness.ValueChanged += new EventHandler(nudValue_Changed);
            this.tbLightness.Tag = trackbLightness;
            
            this.trackbSaturation.ValueChanged += new EventHandler(trackBar_Scroll);
            this.trackbSaturation.Tag = tbSaturation;
            this.tbSaturation.ValueChanged += new EventHandler(nudValue_Changed);
            this.tbSaturation.Tag = trackbSaturation;

            this.Shown += new EventHandler(FormHueSaturationLightness_Shown);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void FormHueSaturationLightness_Shown(object sender, EventArgs e)
        {
            m_ImageOriginal = MainFormSingleton.ActivePictureBox.BackgroundImage;
            this.pictureBox1.BackgroundImage = m_ImageOriginal;
        }

        /// <summary>
        /// Handles the Scroll event of the trackBar control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void trackBar_Scroll(object sender, EventArgs e)
        {
            TrackBar trackBar = sender as TrackBar;
            if (trackBar != null)
            {
                NumericUpDown nudTemp = (NumericUpDown)trackBar.Tag;
                nudTemp.Value = trackBar.Value;
            }
            Bitmap bitmap = (Bitmap)m_ImageOriginal.Clone();
            pictureBox1.BackgroundImage = bitmap;
            AhsbColor(bitmap);
        }

        /// <summary>
        /// Value Changed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void nudValue_Changed(object sender, EventArgs e)
        {
            NumericUpDown nud = sender as NumericUpDown;
            if (nud != null)
            {
                TrackBar trackBarTemp = (TrackBar)nud.Tag;
                trackBarTemp.Value = (int)nud.Value;
            }
        }

        /// <summary>
        /// AhsbColor
        /// </summary>
        /// <param name="bitmap"></param>
        private void AhsbColor(Bitmap bitmap)
        {
            m_AhsbColor.BrightnessShift = trackbLightness.Value / 100.0f;
            m_AhsbColor.HueShift = trackBarHue.Value;
            m_AhsbColor.SaturationShift = trackbSaturation.Value / 100.0f;
            m_AhsbColor.Execute(bitmap).Dispose();
        }

        private void cbOK_Click(object sender, EventArgs e)
        {
            Image image = (Image)m_ImageOriginal.Clone();
            AhsbColor((Bitmap)image);
            MainFormSingleton.ActivePictureBox.BackgroundImage = image;
        }
    }
}
